package com.tianci.de.jni;

import android.util.Log;

import com.skyworth.deservice.DEDefine;
import com.skyworth.deservice.api.def.SRTDEServicesCmdDef.SkyDEServiceCommandEnum;
import com.skyworth.framework.skysdk.util.SkyObjectByteSerialzie;
import com.tianci.de.DEService;
import com.tianci.de.api.DEChannelSession;
import com.tianci.de.api.JsonParams;
import com.tianci.de.api.SkyDEApi;
import com.tianci.de.define.DEPlayerDef;
import com.tianci.de.define.DeviceInfo;

public class DENativeProcessor {
    private final static String TAG = "TCDENativeProc";

    public static String getDeviceInfo(String name) {
        Log.e(TAG, "getDeviceInfo, + name = " + name);
        if ("getDeviceName".equals(name)) {
            return getDeviceName();
        } else if ("getDevType".equals(name)) {
            return getDeviceType();
        } else if ("getPlayInfo".equals(name)) {
            return getPlayInfo();
        } else if ("getAppInfo".equals(name)) {
            return getPlayInfo();
        } else {
            return getInfoCommon(name);
        }
    }

    public static void execAction(String action, String params) {
        Log.i(TAG, "execAction, ++ action = " + action + ", params = " + params);
        if ("pauseOrResume".equals(action)) {
            pauseOrResume();
        } else if ("stop".equals(action)) {
            stop();
        } else if ("seek".equals(action)) {
            seek(params);
        } else if ("setVolume".equals(action)) {
            setVolume(params);
        } else if ("setMute".equals(action)) {
            setMute(params);
        } else {
            execCommon(action, params);
            // Log.e(TAG, "execAction(), action = " + action + ", not deal...");
        }
    }

    private static String getDeviceType() {
        return "AndroidTV";
    }

    public static String getDeviceName() {
        return DeviceInfo.getChineseName();
    }

    public static boolean play(String params) {
        Log.i(TAG, "play, params = " + params);
        // TODO call player
        final DEChannelSession session = getDEChannelSession(
                SkyDEServiceCommandEnum.SKY_COMMAND_PLAYER_PLAY.toString(), params);
        onWriteByDEChannel(session);
        // onWriteByDEChannel(session.toSerialize());
        // DEService.deApi.writeDEChannel(session);
        return true;
    }

    public static void pauseOrResume() {
        Log.i(TAG, "pauseOrResume");
        // TODO
        final DEChannelSession session = getDEChannelSession(
                SkyDEServiceCommandEnum.SKY_COMMAND_PLAYER_PLAY_OR_PAUSE.toString(), "");
        // DEService.deApi.writeDEChannel(session);
        onWriteByDEChannel(session);
    }

    public static void stop() {
        Log.i(TAG, "stop player");
        // TODO
        final DEChannelSession session = getDEChannelSession(
                SkyDEServiceCommandEnum.SKY_COMMAND_PLAYER_STOP.toString(), "");
        onWriteByDEChannel(session);
        // DEService.deApi.writeDEChannel(session);
    }

    public static void seek(String params) {
        Log.i(TAG, "seek, params = " + params);
        // TODO
        final DEChannelSession session = getDEChannelSession(
                SkyDEServiceCommandEnum.SKY_COMMAND_PLAYER_SEEK.toString(), params);
        onWriteByDEChannel(session);
        // DEService.deApi.writeDEChannel(session);
    }

    private static String getPlayInfo() {
        long time = System.currentTimeMillis();
        final DEChannelSession session = getDEChannelSession("getPlayData", "");
        onWriteByDEChannel(session);
        return null;
    }

    private static String getInfoCommon(String name) {
        if (name != null && name.startsWith("SKY_")) {
            final DEChannelSession session = getDEChannelSession(name, null);
            onWriteByDEChannel(session);
        }
        return null;
    }

    private static void execCommon(String action, String params) {
        if (action != null) {
            final DEChannelSession session = getDEChannelSession(action, params);
            onWriteByDEChannel(session);
        }
    }

    private static DEChannelSession getDEChannelSession(String cmd, String params) {
        return new DEChannelSession(new JsonParams(DEPlayerDef.SESSION, DEPlayerDef.TYPE_NOTIFY,
                cmd, params));
    }

    public static void setVolume(String params) {
        Log.i(TAG, "setVolume, parmas = " + params);
        String volume = null;
        // TODO
    }

    public static void setMute(String params) {
        Log.i(TAG, " setMute,  params" + params);
        // TODO
    }

    // 手机端获取信息，需要有返回值
    public static String onReadByDEChannel(String sessionSerialize) {
        DEChannelSession readSession = DEChannelSession.fromSerialize(sessionSerialize);

        String target = DEJniUtil.getCmdTargetBySession(readSession);
        if (target == null) {
            Log.e(SkyDEApi.TAG, "onReadByDEChannel no register session ,sessionSerialize == "
                    + sessionSerialize);
            return "";
        } else {
            if (DEService.getDEService() != null) {
                if (readSession.hasJsonBody()) {
                    byte[] resultByte = SkyDEApi.execCmd(
                            DEService.getDEService(),
                            SkyDEApi.getTargetUri(target,
                                    DEDefine.CmdEnum.CMD_READ_BY_DECHANNEL.toString()),
                            SkyObjectByteSerialzie.toBytes(readSession));
                    Log.i("UIO", "return resultByte:" + resultByte);
                    if (resultByte != null) {
                        DEChannelSession resultsession = SkyObjectByteSerialzie.toObject(
                                resultByte, DEChannelSession.class);
                        Log.i("UIO", "return string:" + resultsession.getJsonBody());
                        return resultsession.getJsonBody();
                    } else {
                        Log.e(SkyDEApi.TAG, "onReadByDEChannel send to target " + target
                                + " but target no response! ");
                    }
                } else {
                    Log.e(SkyDEApi.TAG, "onReadByDEChannel send to target " + target
                            + " but jsonString  == null !! ");
                }
            } else {
                Log.e(SkyDEApi.TAG, " onReadByDEChannel desevice un bind ipcservice!!! ");
            }
        }
        return "";

    }

    // 手机端发送命令，不需要回复该条消息
    public static void onWriteByDEChannel(String sessionSerialize) {
        DEChannelSession readSession = DEChannelSession.fromSerialize(sessionSerialize);
        // onWriteByDEChannel(readSession);
        String target = DEJniUtil.getCmdTargetBySession(readSession);
        if (target == null) {
            Log.e(SkyDEApi.TAG, "onWriteByDEChannel no register session !! " + sessionSerialize);
        } else {
            if (DEService.getDEService() != null) {
                if (readSession.hasJsonBody()) {
                    SkyDEApi.sendCmd(
                            DEService.getDEService(),
                            SkyDEApi.getTargetUri(target,
                                    DEDefine.CmdEnum.CMD_WRITE_BY_DECHANNEL.toString()),
                            SkyObjectByteSerialzie.toBytes(readSession));
                } else {
                    Log.e(SkyDEApi.TAG, "onWriteByDEChannel send to target " + target
                            + " but jsonString  == null !! ");
                }
            } else {
                Log.e(SkyDEApi.TAG, " desevice un bind ipcservice!!! ");
            }
        }
    }

    private static void onWriteByDEChannel(DEChannelSession readSession) {
        String target = DEJniUtil.getCmdTargetBySession(readSession);
        if (target == null) {
            Log.e(SkyDEApi.TAG, "onWriteByDEChannel no register session !! ");
        } else {
            if (DEService.getDEService() != null) {
                if (readSession.hasJsonBody()) {
                    SkyDEApi.sendCmd(
                            DEService.getDEService(),
                            SkyDEApi.getTargetUri(target,
                                    DEDefine.CmdEnum.CMD_WRITE_BY_DECHANNEL.toString()),
                            SkyObjectByteSerialzie.toBytes(readSession));
                } else {
                    Log.e(SkyDEApi.TAG, "onWriteByDEChannel send to target " + target
                            + " but jsonString  == null !! ");
                }
            } else {
                Log.e(SkyDEApi.TAG, " desevice un bind ipcservice!!! ");
            }
        }
    }

}
